/**
 * 
 */
package com.hexacta.hrs.repositories.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.vectrics.user.User;

import com.hexacta.hrs.securitygroups.SecurityGroup;

/**
 * @author mtestasecca
 * @author rmilano
 * 
 */
public class SecurityGroupsRecordObjectRepository extends HibernateRecordObjectRepository<SecurityGroup> {

    /** {@inheritDoc} */
    public SecurityGroupsRecordObjectRepository(Class<SecurityGroup> entityClass) {
        super(entityClass);
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<SecurityGroup> matching(SecurityGroup criteria) {
        Criteria crit = getSession().createCriteria(SecurityGroup.class);

        // Se lo agrega para que solo traiga una sola instacia del securityGroup.
     //   crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        if ((criteria.getUsers()!=null)&&(!criteria.getUsers().isEmpty())) {
        	List<Long> ids = new ArrayList<Long>();
        	for (User user : criteria.getUsers()){
        		ids.add(user.getId());
        	}
        	crit.createCriteria("users").add(Restrictions.in("id", ids));
        }
        
        if(criteria.getName() != null) {
        	crit.add(Restrictions.ilike("name", criteria.getName()));
        }
        
        // Se devuelven los filtros.
        return crit.list();
    }

}